package com.xzy.shop.dao.impl;

import com.xzy.shop.dao.UserDao;
import com.xzy.shop.domain.Admin;
import com.xzy.shop.domain.User;
import com.xzy.shop.util.JDBCUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public void save(User user) {
        String sql = "insert into `user`(username, md5(password), status, code) values (?, ?, ?, ?)";
        template.update(sql, user.getUsername(), user.getPassword(), user.getStatus(), user.getCode());
    }

    @Override
    public User findByUsername(String username) {
        User user = null;
        try {
            String sql = "select * from `user` where username = ? or telephone = ?";
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, username);
        } catch (Exception e) {
        }
        return user;
    }

    @Override
    public User findByCode(String code) {
        User user = null;
        try {
            String sql = "select * from `user` where code = ?";
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), code);
        } catch (Exception e) {
        }
        return user;
    }

    @Override
    public void changeStatus(User user) {
        String sql = "update `user` set status = 'Y' where uid = ?";
        template.update(sql, user.getUid());
    }

    @Override
    public User loginByEmail(String username, String password) {
        User user = null;
        try {
            String sql = "select * from `user` where username = ? and password = ?";
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
        } catch (Exception e) {
        }
        return user;
    }

    @Override
    public User loginByPhone(String username, String password) {
        User user = null;
        try {
            String sql = "select * from `user` where telephone = ? and password = ?";
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
        } catch (Exception e) {
        }
        return user;
    }

    @Override
    public void addInfo(User user, Map<String, String[]> infos) {
        String username = user.getUsername();

        String realname = infos.get("realname")[0];
        String gender = infos.get("gender")[0];
        //获取字符串格式的生日
        String date = infos.get("birthday")[0];
        String[] split = date.split("-");
        Date birthday = new Date(Integer.parseInt(split[0]) - 1900, Integer.parseInt(split[1]), Integer.parseInt(split[2]));
        String sql = "update `user` set realname = ?, gender = ?, birthday = ? where username = ?";
        template.update(sql, realname, gender, birthday, username);
    }

    @Override
    public Admin adminLogin(String username, String password) {
        Admin admin = null;
        try {
            String sql = "select * from admin where username = ? and password = ?";
            admin = template.queryForObject(sql, new BeanPropertyRowMapper<Admin>(Admin.class), username, password);
        } catch (Exception e) {}

        return admin;
    }

    @Override
    public List<User> findAllUser() {
        String sql = "select * from user";
        return template.query(sql, new BeanPropertyRowMapper<User>(User.class));
    }
}
